//
//  UserAccount.swift
//  01-SinaWeibo
//
//  Created by pc on 15/11/11.
//  Copyright © 2015年 file. All rights reserved.
//

import UIKit

class UserAccount: NSObject, NSCoding {
    
    
    //用于调用access_token  接口获取授权后的access token
    var access_token: String?
    
    //生命周期 单位是秒
    var expires_in: NSTimeInterval = 0 {
    
        didSet{
        
            expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    
    }
    
     //过期日期  开发者的过期时间 是 5年  普通用户的过期时间是 3天
    var expires_date: NSDate?
    
    var uid: String?
    
    var avatar_large: String?
    
    var name: String?
    
    
    
    //KVC
    init(dict: [String : AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    //针对于 字典与属性 当字典对应的时候走这个方法
//    override func setValue(value: AnyObject?, forKey key: String) {
//        print(key)
//        
//    }
    //针对于 字典与属性 当字典不对应的时候走这个方法
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
    
//        print(value)
        
    }
    
    override var description: String {
    
        let keys = ["access_token","expires_in","uid","avatar_large","name"]
    
        return dictionaryWithValuesForKeys(keys).description
    
    }
    
    //duixiang
    func saveAccount() {
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        NSKeyedArchiver.archiveRootObject(self, toFile: path)
        print(path)
    }

    
    class func loadAccount() -> UserAccount? {
        //解归档
        //给string对象 变成 路径的这个方法 在 swift 2.0  被 搞丢了
        let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? UserAccount {
            
            //判断用户的token是否过期
            if account.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedAscending{
            return account
        }
            
    }
        return nil
}
    
    
    //MARK：实现NSCoding 协议方法
    //解归档  将 磁盘中的二进制数据 -> 转换成自定义对象  跟反序列化很像
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        uid = aDecoder.decodeObjectForKey("uid") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        name = aDecoder.decodeObjectForKey("name") as? String
    }
    
    //归档 将对象 转换二进制  保存到磁盘中  序列化
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")

    
    
    }
    
    
    
    
    
    
    
    
    
    
    
}
